__author__ = 'vincent Liu'

import smtplib
from email.mime.text import MIMEText

class EmailController:
	def __init__(self):
		self.to_list = []
		self.subject = ""
		self.content = ""
		self.mail_user =""
		self.mail_pass = ""
		self.mail_host = "smtp.xtcapital.net"
		self.html_type = False

	def SendMail(self):
		"""
		:rtype : object
		:param to_list:邮件名单列表
		:param sub: 主题
		:param content: 内容
		:param mail_user: 邮件有户名
		:param mail_host: 邮件服务器
		:param mail_pass: 邮件密码
		:param content_type: 'html' or 'plain'
		:return:bool
		"""
		me = "data checker" + "<" + self.mail_user + ">"
		content_type = "plain"
		if self.html_type == True:
			content_type = "html"
		msg = MIMEText(self.content, _subtype=content_type)
		msg['Subject'] = self.subject
		msg['From'] = me
		msg['To'] = ",".join(self.to_list)  # 将收件人列表以‘；’分隔
		try:
			server = smtplib.SMTP()
			server.connect(self.mail_host)  # 连接服务器
			server.login(self.mail_user, self.mail_pass)  # 登录操作
			server.sendmail(me, self.to_list, msg.as_string())
			server.close()
			return True
		except Exception as e:
			print(str(e))
			return False

